<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace app\common\services;

use app\adminapi\services\maintenance\LangCountryService;
use app\adminapi\services\maintenance\LangService;
use app\adminapi\services\maintenance\LangTranslationService;
use kitego\services\common\CacheService;

class TranslationService
{
    /**
     * 语言包
     * @param string $from 翻译场景
     */
    public function listTranslationJson(string $from = '')
    {
        $langService = app()->make(LangService::class);
        $langCountryService = app()->make(LangCountryService::class);

        // 获取接口传入的语言类型
        if (!$langFlag = request()->header('cb-lang')) {
            // 没有传入则使用系统默认语言显示
            if (!$langFlag = $langService->value(['is_default' => 1], 'lang_flag')) {
                // 系统没有设置默认语言，根据浏览器语言显示，如果浏览器语言在库中找不到，则使用简体中文
                if (request()->header('accept-language') !== null) {
                    $langFlag = explode(',', request()->header('accept-language'))[0];
                } else {
                    $langFlag = 'zh-CN';
                }
            }
        }

        // 获取语言Id
        $langId = $langCountryService->value(['code' => $langFlag], 'lang_id') ?: 1;

        // 获取缓存key
        $langData = $langService->getColumn(['status' => 1, 'is_delete' => 0], 'lang_flag', 'id');


        if (!empty($from) && $from == 'api') {
            $transKey = 'api_translation_' . $langData[$langId];

            // 读取当前语言包
            $translationList = CacheService::remember($transKey, function () use ($langId, $langFlag) {
                return app()->make(LangTranslationService::class)->getColumn([
                    'lang_id' => $langId,
                    'scene' => 2,
                    'is_delete' => 0
                ], 'translation', 'original_text');
            }, 86400, 'translation');
        } else {
            $transKey = 'page_translation_' . $langData[$langId];

            // 读取当前语言包
            $translationList = CacheService::remember($transKey, function () use ($langId, $langFlag) {
                return app()->make(LangTranslationService::class)->getColumn([
                    'lang_id' => $langId,
                    'scene' => 1,
                    'is_delete' => 0
                ], 'translation', 'original_text');
            }, 86400, 'translation');
        }

        if (!empty($from) && $from == 'api') {
            return $translationList;
        }

        return success([
            'langFlag' => $langFlag,
            'langTranslation' => $translationList
        ]);
    }
}